(* dimensions d'une image *)

let get_dims img =
  ((Sdlvideo.surface_info img).Sdlvideo.w, (Sdlvideo.surface_info img).Sdlvideo.h)
 
let color_pixel_black img x y = Sdlvideo.put_pixel_color img x y (0,0,0)

let draw_horizontale_line img intery = begin 
	let (x,y) = get_dims img in
		for i = 0 to (x-1) do	
			color_pixel_black img i intery
		done
end

let draw_verticale_line img interx = begin
	let(x,y) = get_dims img in
		for i = 0 to (y-1) do
			color_pixel_black img interx i
		done
end

let draw_diagonale_line img xdepart ydepart = begin
let i = ref xdepart
and j = ref ydepart
and (x,y) = get_dims img in
		while !i < x && !j < y do begin
			color_pixel_black img (!i) (!j);
			incr j;
		        incr i;
		end
		done;
end

let draw_horizontales img pas = begin
let i = ref 0
and (x,y) = get_dims img in
	while !i < y do begin
		draw_horizontale_line img !i;
		i := !i + pas;
	end
	done;
end

let draw_verticales img pas = begin
let i = ref 0
and (x,y) = get_dims img in
	while !i < x do begin
		draw_verticale_line img !i;
		i := !i + pas;
	end
	done;
end

let draw_diagonales img pas = begin
let i = ref 0
and (x,y) = get_dims img in begin
		while !i < x do begin 
			 draw_diagonale_line img !i 0;
			 i := !i + pas;
		end
		done;
		i := 0;
		while !i < y do begin
			 draw_diagonale_line img 0 !i;
			 i := !i + pas;
		end
		done;
end
end

let intersections img pas = begin
let (x,y) = get_dims img in 
let tab = Array.make (((x*pas)+1)*((y*pas)+1)) (0,0,0) and
i = ref 0 and
k = ref 0 and
j = ref 0 in begin
	while !i < (x-1) do begin
	    while !j < (y-1) do begin
		tab.(!k) <- (!i,!j,0);
		j := !j + pas;
		k := !k + 1;
		end
		done;
		if !j >= y-1 then 
			tab.(!k) <- (!i,y-1,0);
	i := !i+1;
	if !i >= x-1 then
		tab.(!k) <- (x-1,!j,0);
	end
	done;
end
end

let relier_points img (a,b) (c,d) (e,f) = begin (*A refaire avec des whiles*)
	for i = a to c do begin
		for j = b to d do
			color_pixel_black img i j;
		done;
        end
	done;
	for i = a to e do begin
		for j = b to f do 
			color_pixel_black img i j;
		done;
	end
	done;
	for i = c to e do begin
		for j = d to f do
			color_pixel_black img i j;
		done;
	end
	done;
end

(* init de SDL *)

let sdl_init () =
  begin
    Sdl.init [`EVERYTHING];
    Sdlevent.enable_events Sdlevent.all_events_mask;
  end
 
(* attendre une touche ... *)
let rec wait_key () =
  let e = Sdlevent.wait_event () in
    match e with
    Sdlevent.KEYDOWN _ -> ()
      | _ -> wait_key ()
 
(*
  show img dst
  affiche la surface img sur la surface de destination dst (normalement l'écran)
*)
let show img dst =
  let d = Sdlvideo.display_format img in
    Sdlvideo.blit_surface d dst ();
    Sdlvideo.flip dst
 
(* main *)
let main () =
  begin
    (* Nous voulons 1 argument *)
    if Array.length (Sys.argv) < 2 then
      failwith "Il manque le nom du fichier!";
    (* Initialisation de SDL *)
    sdl_init ();
    (* Chargement d'une image *)
    let img = Sdlloader.load_image Sys.argv.(1) in
    (* On récupère les dimensions *)
    let (w,h) = get_dims img in
    (* On crée la surface d'affichage en doublebuffering *) 
    let display = Sdlvideo.set_video_mode w h [`DOUBLEBUF] in
(*      draw_verticales img 10;
      draw_horizontales img 10;
      draw_diagonales img 10;*)
relier_points img (0,0) (50,50) (35,40);
      (* on affiche l'image *)
      show img display;
      (* on attend une touche *)
      wait_key ();
      (* on quitte *)
      exit 0
  end
 
let _ = main ()
